// 落子调试测试
export default class DebugMoveTest {
  constructor() {
    this.openid = null
    this.roomId = null
  }

  // 测试落子功能
  async testMove() {
    try {
      console.log('开始测试落子功能...')

      // 1. 获取OpenID
      await this.getOpenId()

      // 2. 创建房间
      const roomId = await this.createRoom()
      console.log('房间创建成功:', roomId)

      // 3. 模拟第二个玩家加入
      await this.simulateSecondPlayerJoin(roomId)

      // 4. 检查房间状态
      await this.checkRoomStatus(roomId)

      // 5. 测试落子
      await this.testPlacePiece(roomId)

      console.log('落子测试完成')
    } catch (error) {
      console.error('测试失败:', error)
    }
  }

  // 获取OpenID
  async getOpenId() {
    try {
      const { result } = await wx.cloud.callFunction({
        name: 'getOpenId'
      })
      this.openid = result.openid
      console.log('OpenID获取成功:', this.openid)
    } catch (error) {
      console.error('获取OpenID失败:', error)
      throw error
    }
  }

  // 创建房间
  async createRoom() {
    const roomId = 'debug_move_' + Date.now()
    try {
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'create_room',
          roomId: roomId,
          playerName: '测试创建者',
          openid: this.openid
        }
      })

      if (result.success) {
        this.roomId = roomId
        return roomId
      } else {
        throw new Error(result.message)
      }
    } catch (error) {
      console.error('创建房间失败:', error)
      throw error
    }
  }

  // 模拟第二个玩家加入
  async simulateSecondPlayerJoin(roomId) {
    try {
      const secondPlayerOpenid = 'second_player_' + Date.now()
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'join_room',
          roomId: roomId,
          playerName: '测试加入者',
          openid: secondPlayerOpenid
        }
      })

      if (result.success) {
        console.log('第二个玩家加入成功')
      } else {
        throw new Error(result.message)
      }
    } catch (error) {
      console.error('第二个玩家加入失败:', error)
      throw error
    }
  }

  // 检查房间状态
  async checkRoomStatus(roomId) {
    try {
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'get_room_info',
          roomId: roomId
        }
      })

      if (result.success) {
        const roomData = result.data
        console.log('房间状态检查:', {
          status: roomData.status,
          currentPlayer: roomData.currentPlayer,
          player1: roomData.player1,
          player2: roomData.player2,
          myOpenid: this.openid
        })
      }
    } catch (error) {
      console.error('检查房间状态失败:', error)
    }
  }

  // 测试落子
  async testPlacePiece(roomId) {
    try {
      console.log('测试落子: (7, 7)')
      const { result } = await wx.cloud.callFunction({
        name: 'roomManager',
        data: {
          type: 'place_piece',
          roomId: roomId,
          x: 7,
          y: 7,
          openid: this.openid
        }
      })

      console.log('落子结果:', result)

      if (result.success) {
        console.log('✅ 落子成功')
      } else {
        console.log('❌ 落子失败:', result.message)
      }
    } catch (error) {
      console.error('落子测试失败:', error)
    }
  }

  // 运行完整测试
  async runFullTest() {
    console.log('=== 落子调试测试 ===')
    await this.testMove()
    console.log('=== 测试完成 ===')
  }
}
